﻿using System.Diagnostics;
using System.Globalization;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using MockingBird.Common.Infrastructure.Configuration;

namespace MockingBird.Common.Infrastructure.Logging
{
    using System;
    using Microsoft.Practices.EnterpriseLibrary.Logging;

    public class EntLibLogger : LoggerBase
    {
        private const string fileName = "EntLibLogging.Config";
        private LogWriter logWriter;

        #region "constructor(s)"
        public EntLibLogger()
        {
            this.SetDefaults();
            this.SetLogFileName();
            this.ConfigureLogger();
        }

        public EntLibLogger(ILoggingConfigurationProvider targetLoggingConfigProvider)
        {
            this.loggingConfigurationProvider = targetLoggingConfigProvider;
            this.loggerConfigFileFolder = targetLoggingConfigProvider.LoggingConfigurationFolder;
            this.SetLogFileName();
            this.ConfigureLogger();
            Trace.WriteLine("Configuring logger");
        }
        #endregion

        #region ILogger Members

        public override void LogRequestReceived(string endpointUrl)
        {
            string logString = String.Format(
                CultureInfo.CurrentCulture,
                "{0} {1} REQUEST RECEIVED FOR URL : {2}",
                "---------------------------------------------------------------",
                Environment.NewLine,
                endpointUrl);
            this.Log(logString, LoggingCategory.General, LoggingPriority.High);
            Trace.WriteLine(logString);

        }

        public override void Log(string message)
        {
            this.Log(message, LoggingCategory.General, LoggingPriority.Normal);
            Trace.WriteLine(message);
        }

        public override void LogInfo(string message)
        {
            this.Log(message, LoggingCategory.Info, LoggingPriority.Normal);
            Trace.WriteLine(message);
        }

        public override void LogWarning(string message)
        {
            this.Log(message, LoggingCategory.Warning, LoggingPriority.High);
            Trace.WriteLine(message);
        }

        public override void LogError(string message, Exception ex)
        {
            this.Log(message + Environment.NewLine + ex.ToString(), LoggingCategory.Error, LoggingPriority.High);
            Trace.WriteLine(message);
        }

        public override void LogFatal(string message, Exception ex)
        {
            this.Log(message + Environment.NewLine + ex.ToString(), LoggingCategory.Error, LoggingPriority.Highest);
            Trace.WriteLine(message);
        }

        #endregion

        #region "private methods"

        private void SetDefaults()
        {
            this.loggerConfigFileFolder = ConfigurationUtils.GetSiblingConfigFolder();
        }

        private void SetLogFileName()
        {
            this.loggingConfigFile = Path.Combine(this.loggerConfigFileFolder, fileName);
        }

        private void ConfigureLogger()
        {
            IConfigurationSource loggingConfigSource = new LoggingConfigurationSource(loggingConfigFile);
            LogWriterFactory factory = new LogWriterFactory(loggingConfigSource);
            this.logWriter = factory.Create();
        }

        private void Log(string message, LoggingCategory loggingCategory, LoggingPriority loggingPriority)
        {
            LogEntry entry = new LogEntry();
            entry.Message = message;
            entry.Categories.Add(loggingCategory.ToString());
            entry.Priority = (int)Enum.Parse(typeof(LoggingPriority), loggingPriority.ToString());

            this.logWriter.Write(entry);
        }


        #endregion
    }
}
